#!/bin/bash

RMV="ssl/ssl_task.c \
	crypto/ripemd/asm/rips.cpp \
	crypto/x509v3/v3conf.c \
	crypto/LPdir_vms.c crypto/LPdir_nyi.c crypto/LPdir_win*.c \
	crypto/bf/bfs.cpp \
	crypto/bio/bss_rtcp.c \
	crypto/bn/exp.c \
	crypto/cast/casts.cpp \
	crypto/arm_arch.h \
	crypto/armcap.c \
	crypto/ppc_arch.h \
	crypto/ppccap.c \
	crypto/s390xcap.c \
	crypto/sparc_arch.h \
	crypto/sparcv9cap.c \
	crypto/vms_rms.h \
	crypto/des/times \
	crypto/des/read_pwd.c \
	crypto/store \
	crypto/jpake \
	crypto/md2 \
	crypto/evp/e_dsa.c \
	crypto/rc5 \
 	crypto/bf/bf_opts.c \
 	crypto/bf/bfspeed.c \
 	crypto/bn/bnspeed.c \
 	crypto/bn/expspeed.c \
 	crypto/cast/cast_spd.c \
 	crypto/cast/castopts.c \
 	crypto/conf/cnf_save.c \
 	crypto/des/des.c \
 	crypto/des/des3s.cpp \
 	crypto/des/des_opts.c \
 	crypto/des/dess.cpp \
 	crypto/des/rpw.c \
 	crypto/des/speed.c \
 	crypto/dh/p1024.c \
 	crypto/dh/p192.c \
 	crypto/dh/p512.c \
 	crypto/dsa/dsagen.c \
 	crypto/idea/idea_spd.c \
 	crypto/md4/md4.c \
 	crypto/md4/md4s.cpp \
 	crypto/md5/md5.c \
 	crypto/md5/md5s.cpp \
 	crypto/modes/cts128.c \
 	crypto/rc2/rc2speed.c \
 	crypto/rc4/rc4.c \
 	crypto/rc4/rc4s.cpp \
 	crypto/rc4/rc4speed.c \
 	crypto/ripemd/rmd160.c \
 	crypto/sha/sha.c \
 	crypto/sha/sha1.c \
 	crypto/ec/ecp_nistz256.c \
 	crypto/ec/ecp_nistz256_table.c \
 	crypto/aes/aes_x86core.c \
 	crypto/bf/bf_cbc.c \
 	crypto/rc2/tab.c \
 	crypto/sha/sha256t.c \
 	crypto/sha/sha512t.c \
 	crypto/x509v3/v3prin.c \
 	engines/ccgost/gostsum.c \
 	crypto/bn/asm/x86_64-gcc.c \
 	crypto/bn/vms-helper.c"

cd COpenSSL
rm -rf $RMV

find . -iname "*test.c" -exec rm {} \;

find . -type f \( -iname "*.h" -o -iname "*.c" \) -exec sed -i '' -e 's#include <openssl/#include <#g' {} \;
find . -type f \( -iname "*.h" -o -iname "*.c" \) -exec sed -i '' -e 's#include "openssl/#include "#g' {} \;
find . -type f \( -iname "*.h" -o -iname "*.c" \) -exec sed -i '' -e 's#include "../../#include "#g' {} \;
find . -type f \( -iname "*.h" -o -iname "*.c" \) -exec sed -i '' -e 's#include "../crypto/#include "#g' {} \;
find . -type f \( -iname "*.h" -o -iname "*.c" \) -exec sed -i '' -e 's#include "vendor_defns/#include "#g' {} \;

mv crypto/LPdir_unix.c crypto/LPdir_unix.h
mv crypto/pkcs7/pk7_enc.c crypto/pkcs7/pk7_enc.h
mv crypto/des/ncbc_enc.c crypto/des/ncbc_enc.h

mkdir -p openssl
rm openssl/*
cd openssl
find ../crypto -iname "*.h" -exec mv {} . \;
find ../ssl -iname "*.h" -exec mv {} . \;
find ../engines -iname "*.h" -exec mv {} . \;
cd ..

mv openssl/* .

sed -i '' -e 's#include "LPdir_unix.c"#include "LPdir_unix.h"#g' crypto/o_dir.c
sed -i '' -e 's#include "ncbc_enc.c"#include "ncbc_enc.h"#g' crypto/des/cbc_enc.c
sed -i '' -e 's#include "ncbc_enc.c"#include "ncbc_enc.h"#g' crypto/des/des_enc.c

cd openssl
find ../crypto -iname "*.c" -exec mv {} . \;
find ../ssl -iname "*.c" -exec mv {} . \;
find ../engines -iname "*.c" -exec mv {} . \;
cd ..

mv openssl/* .

rm -rf openssl

for header in *.h
do
	GRP=`grep -l -s "include <$header>" *`
	for fnd in $GRP
	do
		sed -i '' -e "s#include <$header>#include \"$header\"#g" $fnd;
	done
done

sed -i '' -e "s#define BN_MASK #define BN_MASK //#g" bn.h

# HEADERS="aes.h bn.h comp.h dh.h ec.h hmac.h md5.h opensslconf.h pkcs7.h rsa.h ssl.h tls1.h x509.h \
# 		asn1.h buffer.h conf.h dsa.h ecdh.h idea.h mdc2.h opensslv.h pqueue.h safestack.h   ssl2.h ts.h x509_vfy.h \
# 		asn1_mac.h camellia.h conf_api.h dso.h ecdsa.h krb5_asn.h modes.h ossl_typ.h rand.h seed.h ssl23.h txt_db.h x509v3.h \
# 		asn1t.h cast.h crypto.h dtls1.h engine.h kssl.h obj_mac.h pem.h rc2.h sha.h ssl3.h ui.h \
# 		bio.h cmac.h des.h e_os2.h err.h lhash.h objects.h pem2.h rc4.h srp.h stack.h ui_compat.h \
# 		blowfish.h cms.h des_old.h ebcdic.h evp.h md4.h ocsp.h pkcs12.h ripemd.h srtp.h symhacks.h whrlpool.h"
# 
# mkdir -p include/Headers
# cd include/Headers
# rm *.h
# 
# for header in $HEADERS
# do
# 	cp "../../$header" .
# done

# for header in *.h
# do
# 	GRP=`grep -l -s "include \"$header\"" *`
# 	for fnd in $GRP
# 	do
# 		sed -i '' -e "s#include \"$header\"#include \"OpenSSL/$header\"#g" $fnd;
# 	done
# done